package com.sgb.kjwl.view.ui.adapter.home;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sgb.kjwl.R;
import com.sgb.kjwl.view.ui.activity.home.SearchActivity;
import com.swgk.core.recyclerview.secondary.CommonHolder;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.List;

public class SearchAdapter extends RecyclerView.Adapter {

    private final Context mContext;
    private int count = 2;
    private int TYPE_ONE = 1;
    private int TYPE_TWO = 2;
    private List<String> hotList;
    private List<String> historyList;
    private boolean del = false;
    private SearchActivity activity;

    public SearchAdapter(Context context, SearchActivity activity) {
        this.mContext = context;
        this.activity = activity;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder = null;
        if (viewType == TYPE_ONE) {
            viewHolder = CommonHolder.get(mContext, parent, R.layout.holder_search_history);
        } else if (viewType == TYPE_TWO) {
            viewHolder = CommonHolder.get(mContext, parent, R.layout.holder_search_hot);
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (position == 0) {
            initOne((CommonHolder) holder);
        } else if (position == 1) {
            initTwo((CommonHolder) holder);
        }
    }

    private TagAdapter historyTagAdapter;

    //最近搜索
    private void initOne(CommonHolder holder) {
        if (historyTagAdapter == null) {
            final LayoutInflater mInflater = LayoutInflater.from(mContext);
            TagFlowLayout flowLayout = holder.getItem().findViewById(R.id.flow_layout_history);
            ImageView delView = holder.getItem().findViewById(R.id.del_view);
            LinearLayout rightLayout = holder.getItem().findViewById(R.id.right_layout);
            TextView comlapteView = holder.getItem().findViewById(R.id.complate_view);
            TextView allDelView = holder.getItem().findViewById(R.id.all_del_view);
            delView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    delView.setVisibility(View.GONE);
                    rightLayout.setVisibility(View.VISIBLE);
                    activity.chooseSearchHistory(true);
                }
            });
            comlapteView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    rightLayout.setVisibility(View.GONE);
                    delView.setVisibility(View.VISIBLE);
                    activity.chooseSearchHistory(false);
                }
            });
            allDelView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    activity.delAllSearchHistory();
                }
            });
            historyTagAdapter = new TagAdapter<String>(historyList) {

                @Override
                public View getView(FlowLayout parent, int position, String s) {
                    RelativeLayout layout = (RelativeLayout) mInflater.inflate(R.layout.flow_layout_history, flowLayout, false);
                    TextView tv = layout.findViewById(R.id.name_view);
                    tv.setText(s);
                    ImageView imageView = layout.findViewById(R.id.del_img);
                    imageView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            activity.delSearchHistory(s);
                        }
                    });
                    if (del) {
                        imageView.setVisibility(View.VISIBLE);
                    } else {
                        imageView.setVisibility(View.GONE);
                    }
                    return layout;
                }
            };
            flowLayout.setAdapter(historyTagAdapter);
            flowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
                @Override
                public boolean onTagClick(View view, int position, FlowLayout parent) {
                    activity.search(historyList.get(position));
                    return true;
                }
            });
        }
    }

    private TagAdapter hotTagAdapter;

    //热门搜索
    private void initTwo(CommonHolder holder) {
        if (hotTagAdapter == null) {
            final LayoutInflater mInflater = LayoutInflater.from(mContext);
            TagFlowLayout flowLayout = holder.getItem().findViewById(R.id.flow_layout_hot);
            hotTagAdapter = new TagAdapter<String>(hotList) {

                @Override
                public View getView(FlowLayout parent, int position, String s) {
                    TextView tv = (TextView) mInflater.inflate(R.layout.flow_layout_tv, flowLayout, false);
                    tv.setText((position + 1) + "." + s);
                    return tv;
                }
            };
            flowLayout.setAdapter(hotTagAdapter);
            if (hotList != null && hotList.size() > 2) {
                hotTagAdapter.setSelectedList(0, 1, 2);
            }
            flowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
                @Override
                public boolean onTagClick(View view, int position, FlowLayout parent) {
                    if (hotList != null && hotList.size() > 2) {
                        hotTagAdapter.setSelectedList(0, 1, 2);
                    }
                    activity.search(hotList.get(position));
                    return true;
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_ONE;
        } else {
            return TYPE_TWO;
        }
    }

    @Override
    public int getItemCount() {
        return count;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {

                //如果快速滑动， 不加载图片
                if (newState == 2) {
                    Glide.with(mContext).pauseRequests();
                } else {
                    Glide.with(mContext).resumeRequests();
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

            }
        });
    }

    public void setSearchHistory(List<String> historyList) {
        historyTagAdapter = null;
        this.historyList = historyList;
        notifyDataSetChanged();
    }

    public void setChooseSearchHistory(boolean del) {
        historyTagAdapter = null;
        this.del = del;
        notifyDataSetChanged();
    }

    public void setSearchHot(List<String> hotList) {
        hotTagAdapter = null;
        this.hotList = hotList;
        notifyDataSetChanged();
    }
}
